package com.example.smart.handler;

import cn.hutool.crypto.SecureUtil;
import com.example.common.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class HutoolPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        String salt = Utils.salt();
        return SecureUtil.md5(rawPassword.toString() + salt);
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        String salt = Utils.salt();
        String md5Password = SecureUtil.md5(rawPassword.toString() + salt);
        return md5Password.equals(encodedPassword);
    }
    private String encodeWithSalt(CharSequence rawPassword, String salt) {
        return SecureUtil.md5(rawPassword.toString() + salt);
    }

    private String extractSaltFromEncodedPassword(String encodedPassword) {
        int saltLength = Utils.salt().length();
        return encodedPassword.substring(encodedPassword.length() - saltLength);
    }

}
